package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.qigao.mall.api.BillItemService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.BillItemEnum;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.admin.merchant.*;
import com.qigao.mall.web.vo.app.WithDrawQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/billItem/")
@Api(tags = "Admin商户费用项相关API" ,value = "Admin商户操作费用项相关API")
public class AdminBillItemController extends AbstractExcelController {
    private static Logger logger= LoggerFactory.getLogger(AdminBillItemController.class);
    @Autowired
    BillItemService tbBillItemService;


    /**
     * restful api 增删改查
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ApiOperation(value = "创建费用项", notes = "根据费用项对象创建费用项")
    @ApiParam(name = "sysBillItemCreateVo", value = "费用项信息实体 sysBillItemCreateVo", required = true)
    public Result<String> create(@RequestBody TbBillItemCreateVo tbBillItemCreateVo, HttpServletRequest request) {
        ValidatorUtil.validateEntity(tbBillItemCreateVo);//校验
        try {
            TbBillItemCreateDto tbBillItemCreateDto = new TbBillItemCreateDto();
            BeanUtils.copyProperties(tbBillItemCreateVo, tbBillItemCreateDto);
            if(StringUtils.isNotEmpty(tbBillItemCreateVo.getItemCode())){
                tbBillItemCreateDto.setItemName(BillItemEnum.getNameByCode(tbBillItemCreateVo.getItemCode()));
            }
            tbBillItemCreateDto.setCreateUserNo(RequestUtils.getCurrentUserNo(request));
            return tbBillItemService.create(tbBillItemCreateDto);
        } catch (Exception e) {
            logger.error("添加费用项异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @ApiOperation(value = "更新费用项详细信息", notes = "根据url的费用项编号来指定更新对象，并根据传过来的费用项信息来更新费用项详细信息")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result<String> update(@RequestBody TbBillItemModiVo tbBillItemModiVo, HttpServletRequest request) {
        ValidatorUtil.validateEntity(tbBillItemModiVo);//校验
        try {
            TbBillItemModiDto tbBillItemModiDto = new TbBillItemModiDto();
            BeanUtils.copyProperties(tbBillItemModiVo, tbBillItemModiDto);
            tbBillItemModiDto.setId(Long.valueOf(tbBillItemModiVo.getId()));
            if(StringUtils.isNotEmpty(tbBillItemModiVo.getItemCode())){
                tbBillItemModiDto.setItemName(BillItemEnum.getNameByCode(tbBillItemModiVo.getItemCode()));
            }
            tbBillItemModiDto.setModiUserNo(RequestUtils.getCurrentUserNo(request));
            return tbBillItemService.update(tbBillItemModiDto);
        } catch (Exception e) {
            logger.error("修改费用项异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除费用项", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "费用项编号", required = true, dataType = "string", paramType = "path")
    public Result<String> delete(@PathVariable("id") String id) {
        try {
            return tbBillItemService.delete(id);
        } catch (Exception e) {
            logger.error("删除费用项异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @RequestMapping(value = "listPage", method = RequestMethod.POST)
    @ApiOperation(value = "费用项分页查询", notes = "费用项分页查询")
    @ApiParam(name = "sysBillItemCondVo", value = "费用项分页查询", required = true)
    public Result listPage(@RequestBody TbBillItemCondVo tbBillItemCondVo) {
        ValidatorUtil.validateEntity(tbBillItemCondVo);//校验
        try {
            TbBillItemCondDto tbBillItemCondDto = new TbBillItemCondDto();
            BeanUtils.copyProperties(tbBillItemCondVo, tbBillItemCondDto);
            Page<TbBillItemResultDto> tbBillItemResultDtos = tbBillItemService.listPage(tbBillItemCondDto);
            PageInfo pageInfo = new PageInfo(tbBillItemResultDtos);
            return Result.newSuccess(pageInfo);
        } catch (Exception e) {
            logger.error("费用项分页查询异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "/findByItemCode/{itemCode}", method = RequestMethod.POST)
    @ApiOperation(value = "按照费用项获取角色列表", notes = "按照费用项获取角色列表")
    @ApiImplicitParam(name = "itemCode", value = "费用项编号", required = true, dataType = "string", paramType = "path")
    public Result<List<TbBillOwnerResultDto>> findByItemCode(@PathVariable("itemCode") String itemCode) {
        try {
            return tbBillItemService.findByItemCode(itemCode);
        } catch (Exception e) {
            logger.error("按照费用项获取角色列表异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "/save/", method = RequestMethod.POST)
    @ApiOperation(value = "保存费用项角色", notes = "批量保存费用项角色")
    @ApiParam(name = "tbBillOwnerUpdateVo", value = "费用项角色信息实体 tbBillOwnerUpdateVo", required = true)
    public Result<String> save(@RequestBody TbBillOwnerUpdateVo tbBillOwnerUpdateVo, HttpServletRequest request) {
        ValidatorUtil.validateEntity(tbBillOwnerUpdateVo);
        try {
            TbBillOwnerUpdateDto tbBillOwnerUpdateDto=new TbBillOwnerUpdateDto();
            tbBillOwnerUpdateDto.setItemCode(tbBillOwnerUpdateVo.getItemCode());
            List<TbBillOwnerDto> tbBillOwnerDtos = Lists.newArrayList();
            if(!CollectionUtils.isEmpty(tbBillOwnerUpdateVo.getTbBillOwnerVos())){
                for (TbBillOwnerVo tbBillOwnerVo : tbBillOwnerUpdateVo.getTbBillOwnerVos()) {
                    TbBillOwnerDto tbBillOwnerDto = new TbBillOwnerDto();
                    tbBillOwnerDto.setItemCode(tbBillOwnerUpdateVo.getItemCode());
                    tbBillOwnerDto.setItemName(tbBillOwnerUpdateVo.getItemName());
                    tbBillOwnerDto.setOwnerType(tbBillOwnerVo.getOwnerType());
                    tbBillOwnerDto.setOwnerTypeName(tbBillOwnerVo.getOwnerTypeName());
                    tbBillOwnerDto.setOperUserNo(RequestUtils.getCurrentUserNo(request));
                    tbBillOwnerDtos.add(tbBillOwnerDto);
                }
            }
            tbBillOwnerUpdateDto.setTbBillOwnerDtos(tbBillOwnerDtos);
            return tbBillItemService.save(tbBillOwnerUpdateDto);
        } catch (Exception e) {
            logger.error("保存费用项角色异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(HttpServletResponse response) {
        TbBillItemCondDto tbBillItemCondDto = new TbBillItemCondDto();
        tbBillItemCondDto.setPageNum(1);
        tbBillItemCondDto.setPageNum(999999999);
        // 待导出数据
        List<TbBillItemResultDto> billItemResultDtos = tbBillItemService.listAll(tbBillItemCondDto);
        ExcelUtils excelUtils = new ExcelUtils(billItemResultDtos, getHeaderInfo(), null);
        sendHttpResponse(response, "费用项配置报表", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "角色"),
                new ExcelHeaderInfo(0, 0, 1, 1, "级别"),
                new ExcelHeaderInfo(0, 0, 2, 2, "费用项"),
                new ExcelHeaderInfo(0, 0, 3, 3, "费用项名称"),
                new ExcelHeaderInfo(0, 0, 4, 4, "分润模式"),
                new ExcelHeaderInfo(0, 0, 5, 5, "分润值")
        );
    }




}
